import 'dart:convert';

import 'package:manager/http/entity/entity_factory.dart';
import 'package:flutter/foundation.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:manager/configs/sp_key.dart';
import 'package:shared_preferences/shared_preferences.dart';

class PreferenceService extends GetxService {
  late SharedPreferences _preferences;
  GetStorage? _storage;

  Future<PreferenceService> init() async {
    await GetStorage.init('com.foxconn.manager');
    _storage = GetStorage('com.foxconn.manager');
    return this;
  }

  @override
  void onInit() async {
    _preferences = await SharedPreferences.getInstance();
    super.onInit();
  }

  ///获取用户id
  String userId() {
    return getData<String>(SpKey.user_id);
  }

  ///将数据写入内存中
  void writeData(String key, dynamic data) async {
    await _storage?.write(key, data);
  }

  ///从内存中读取数据
  T? readData<T>(String key) {
    return _storage?.read<T>(key);
  }

  ///从内存中移除数据
  void removeData<T>(String key) {
    _storage?.remove(key);
  }

  void watchData(String key, Function(dynamic) callBack) {
    _storage?.listenKey(key, callBack);
  }

  ///移除单个监听器
  void removeListener(ValueSetter listener) {
    _storage?.removeKeyListener(listener);
  }

  ///移除监听器集合
  void removeListenerList(Set<ValueSetter> listeners) {
    _storage?.removeListenerList(listeners);
  }

  ///擦除数据,因为在内存中所以每次app退出都会自动擦除
  ///但是如果用户退出登录需要手动调用擦除数据
  void eraseData() async {
    await _storage?.erase();
  }

  ///保存基本类型的数据
  void saveData(String key, dynamic data) async {
    switch (data.runtimeType.toString()) {
      case 'String':
        await _preferences.setString(key, data as String);
        break;
      case 'int':
        await _preferences.setInt(key, data as int);
        break;
      case 'double':
        await _preferences.setDouble(key, data as double);
        break;
      case 'bool':
        await _preferences.setBool(key, data as bool);
        break;
      default:
        _preferences.setString(key, json.encode(data));
        break;
    }
  }

  ///从本地移除保存的数据
  void clearData(String key) {
    _preferences.remove(key);
  }

  ///获取基本数据类型的方法
  T getData<T>(String key, {T? defaultValue}) {
    if (_preferences.get(key) == null) {
      if (defaultValue == null) {
        switch (T.toString()) {
          case 'String':
            return '' as T;
          case 'int':
            return 0 as T;
          case 'double':
            return 0.0 as T;
          case 'bool':
            return false as T;
        }
      } else {
        return defaultValue;
      }
    }
    return _preferences.get(key) as T;
  }

  ///获取自定义数据类型的方法
  T? getObject<T>(String key, {T? defaultValue}) {
    var data = _preferences.get(key);
    if (data == null) {
      if (defaultValue == null) {
        return null;
      } else {
        return defaultValue;
      }
    }

    return EntityFactory.convertOBJ<T>(
        json.decode(data.toString()) as Map<String, dynamic>?);
  }
}
